/* eslint-disable default-case */
import { message } from 'antd';
import {
  getDownloadInfo,
  getDownloadInfoTax,
  getUploadProgress,
  customerList,
  uploadFileSend,
  uploadFileTax,
} from '@/services/salaybatchns';
import * as service from '@/services/socket';

export default {
  state: {
    batchQuery: {
      // 批量上传数据
      progressPercent: 0,
      wageFalg: '2', // 导入类型	2、待发放。4、待报税、

      // 接单方待发放工资导入
      ordSndrId: undefined,
      custId: undefined,
      custName: undefined,
      incmType: undefined,
      busiYm: undefined,
      wageDate: undefined,
      changeFlag: '2', // 是否变更发放日期
      wageReaSendDate: undefined,
      reaRsn: undefined,
    },
    custNameList: [],
    supplyList: [],
    isGoBack: false,
    currentStep: 0,
    progressPercent: 0,
    resultInfo: {
      batchName: '',
      taskCount: 0, // 导入数据
      successCount: 0, // 成功数据
      errorCount: 0, // 失败数据
      batchNo: '', // 批次号
    },
    showProgress: false,
    batchTaskType: 'send',
  },
  effects: {
    // 客户名称列表
    *customerList({ payload, callback }, { call }) {
      const response = yield call(customerList, payload);
      if (callback) {
        callback(response);
      }
    },
    // 待发放获取下载模板fileId fileName
    *getDownloadInfo({ payload }, { call, put }) {
      const response = yield call(getDownloadInfo, payload);
      yield put({
        type: 'updateQuery',
        payload,
      });
      if (response.success) {
        const fileInfo = response.data.data.split('|');
        yield put({
          type: 'publicdata/setFileDownload',
          payload: { fileId: fileInfo[0], fileName: fileInfo[1] },
        });
      }
    },
    // 待报税获取下载模板fileId fileName
    *getDownloadInfoTax({ payload }, { call, put }) {
      const response = yield call(getDownloadInfoTax, payload);
      yield put({
        type: 'updateQuery',
        payload,
      });
      if (response.success) {
        const fileInfo = response.data.data.split('|');
        yield put({
          type: 'publicdata/setFileDownload',
          payload: { fileId: fileInfo[0], fileName: fileInfo[1] },
        });
      }
    },
    // 获取上传进度数据
    *getUploadProgress({ payload, callback }, { call, put }) {
      const response = yield call(getUploadProgress, payload);
      if (response.success) {
        yield put({
          type: 'updateQuery',
          payload: { progressPercent: response.data.successProp },
        });
      }
      if (callback && typeof callback === 'function') {
        callback(response);
      }
    },
    // 上传文件:批量进度
    *uploadFileWithProgress({ payload }, { call, put }) {
      let api = null;
      if (payload.type === 'send') {
        api = uploadFileSend;
      }
      if (payload.type === 'tax') {
        api = uploadFileTax;
      }
      yield put({
        // 区分导入还是待发放还是待保税
        type: 'updateModelData',
        payload: {
          batchTaskType: payload.type,
        },
      });
      const response = yield call(api, payload.formData);
      if (response.success) {
        if (response.data && response.data.successProp !== 100) {
          yield call(service.socketChannel, {
            type: 'sub',
            data: { to: response.data && (response.data.batchId || response.data), type: 'task' },
          });
        } else {
          yield put({
            type: 'updateBatchTask',
            payload: response.data,
          });
        }
      } else {
        // 上传失败
        message.warning(`${response.message || '上传失败'}`);
      }
      return response;
    },
  },

  reducers: {
    // 获取列表
    getList(
      state,
      {
        payload: { data, currentPage, pageSize },
      }
    ) {
      const list = Array.isArray(data.pageRecords) ? data.pageRecords : [];
      return {
        ...state,
        data: {
          list,
          pagination: { total: data.totalCount, current: currentPage, pageSize },
        },
      };
    },
    // 设置修改当前state数据
    updateModelData(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    // 更新批量上传参数
    updateQuery(state, { payload }) {
      return {
        ...state,
        batchQuery: { ...state.batchQuery, ...payload },
      };
    },
    // 更新批量任务数据
    updateBatchTask(state, { payload }) {
      let { showProgress, resultInfo, currentStep } = state;
      const { batchTaskType, batchQuery } = state;
      showProgress = true;
      if (payload.successProp === 100) {
        // 下载错误数据：地址
        if (batchTaskType === 'send') {
          resultInfo.errorUrl = '/csApi/wageDowoload/dowoloadErrorWage';
          // 下载错误数据：参数
          resultInfo.errorParams = {
            batchId: payload.batchId,
            incmType: batchQuery.incmType,
          };
        } else {
          resultInfo.errorUrl = '/csApi/wageDowoload/dowoloadErrorTax';
          // 下载错误数据：参数
          resultInfo.errorParams = {
            batchId: payload.batchId,
          };
        }
        resultInfo.errorParams = {
          batchId: payload.batchId,
        };
        resultInfo = { ...resultInfo, ...payload };
        currentStep += 1;
        showProgress = false;
      }
      return {
        ...state,
        showProgress,
        progressPercent: payload.successProp,
        resultInfo,
        currentStep,
      };
    },
  },
  subscriptions: {
    socket({ dispatch }) {
      return service.listen(action => {
        switch (action.type) {
          case 'task':
            dispatch({
              type: 'updateBatchTask',
              payload: action.data,
            });
            // 全局保存
            dispatch({
              type: 'publicdata/updateBatchTaskData',
              payload: action.data,
            });
            break;
        }
      });
    },
  },
};
